package com.example.video.controller;

import com.example.video.entity.Device;
import com.example.video.service.IDeviceService;
import com.example.video.service.impl.AccountServiceImpl;
import com.example.video.service.impl.OuterAccountServiceImpl;
import com.example.video.service.impl.VideoLogServiceImpl;
import com.example.video.service.impl.VideoServiceImpl;
import com.example.video.common.Result;

import com.example.video.util.laixiOperate.LAIXICmd;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @desc 公共接口
 * */
@Slf4j
@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    private AccountServiceImpl accountService;
    @Resource
    private OuterAccountServiceImpl outerAccountService;
    @Resource
    private VideoServiceImpl videoService;
    @Resource
    private VideoLogServiceImpl videoLogService;



    @Resource
    private IDeviceService deviceService;


    @Value("${ServerUrl}")
    private String serverUrl;



    @Resource
    private LAIXICmd laixiCmd;

    /* ************ */
    /*   源账户部分   */
    /* ************ */









    /* ************* */
    /* 视频上传日志部分 */
    /* ************* */
    /**
     * @desc 通过页数和大小获取目标账号列表
     * @param page 页数
     * @param limit 每页大小
     * @return 获取的账户列表
     */
    
    /*@PostMapping("/videoLogList")
    public Result<List<VideoLog>> getVideoLogList(Integer page, Integer limit){
        Result<List<VideoLog>> rs = new Result<>();
        List<VideoLog> list = videoLogService.getList(page, limit) ;
        if( list.isEmpty() ){
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("获取失败！");
        }else{
            rs.setCode(0);
            rs.setData(list);
            rs.setMsg("获取成功！");
        }
        rs.setCount(list.size());
        return rs;
    }
    */

    /**
     * @desc 通过ID获取账户项信息
     * @param id 账户项的ID，用于查询特定的账户项
     * @return 返回一个Result对象，其中包含账户项信息和操作结果的状态码、消息
     */
    /*
    @PostMapping("/videoLogItem")
    public Result<VideoLog> getVideoLogItem(Integer id){
        Result<VideoLog> rs = new Result<>();
        VideoLog item = videoLogService.getItem(id);
        if(item != null){
            rs.setData(item);
            rs.setMsg("获取成功！");
            rs.setCode(0);
        }else {
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("获取失败！");
        }
        rs.setCount(0);
        return rs;
    }
    */

    /* ************* */
    /*      设备       */
    /* ************* */


    @PostMapping("/search/devices")
    public Result<List<Map<String, String>>> searchDevices(Integer page, Integer limit, Authentication authentication,
                                                           @RequestParam(defaultValue = "1") Integer status
                                                           ){
        Result<List<Map<String, String>>> rs = new Result<>();
        List<Device> list = deviceService.getListByStatus(page, limit, status);
        if( list.isEmpty() ){
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("获取失败！无数据！");
            rs.setCount(0);
        }else{
            List<Map<String, String>> devices = new ArrayList<>();
            log.info("ApiCtl -> search/devices ->msg: "+list);
            list.forEach(item->{
                Map<String, String> map = new HashMap<>(1);
                map.put("id",item.getId().toString());
                map.put("deviceId",item.getDeviceId());
                map.put("name",accountService.getItemByDeviceId(item.getDeviceId()).getName());
                devices.add(map);
            });

            rs.setCode(0);
            rs.setData(devices);
            rs.setMsg("获取成功！");
            rs.setCount(deviceService.getCountByROle(authentication.getAuthorities().toString()));
        }
        return rs;
    }



}
